package com.bifan.txtreaderlib.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bifan.txtreaderlib.R;
import com.bifan.txtreaderlib.interfaces.IChapter;

import java.util.List;

/**
 * 章节界面
 */
public class ChapterList extends PopupWindow {
    private Context             mContext;
    private RecyclerView        mRootView;
    private MyAdapter           mAdapter;
    private List<IChapter>      mChapters;
    private int                 CurrentIndex = -1;
    private int                 AllCharNum;
    private int                 ViewHeight;
    private OnItemClickListener onClick;

    public ChapterList(Context mContext, int ViewHeight, List<IChapter> mChapters, int allCharNum) {
        super(mContext);
        this.mContext = mContext;
        this.ViewHeight = ViewHeight;
        this.mChapters = mChapters;
        this.AllCharNum = allCharNum;
        initRootView();
    }

    public int getAllCharNum() {
        return AllCharNum;
    }

    public void setCurrentIndex(int currentIndex) {
        CurrentIndex = currentIndex;
    }

    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    public RecyclerView getRecyclerView() {
        return mRootView;
    }

    public RecyclerView.Adapter<MyAdapter.Holder> getAdapter() {
        return mAdapter;
    }

    public void setOnItemClickListener(OnItemClickListener onClick) {
        this.onClick = onClick;
    }

    protected void initRootView() {
        WindowManager m = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        m.getDefaultDisplay().getMetrics(metrics);

        int ViewHeight = this.ViewHeight;
        int ViewWidth = metrics.widthPixels;
        mRootView = new RecyclerView(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mRootView.setLayoutParams(params);
        mRootView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        this.setContentView(mRootView);
        this.setWidth(ViewWidth);
        this.setHeight(ViewHeight);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(R.style.TxtChapterMenuAnimation);
        this.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f1f6b8")));
        mAdapter = new MyAdapter();
        mRootView.setAdapter(mAdapter);
    }

    public void setBackGroundColor(int color) {
        mRootView.setBackgroundColor(color);
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(mContext).inflate(R.layout.adapter_chapterlist, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            final IChapter chapter = mChapters.get(position);
            if (CurrentIndex == position) {
                holder.progress.setTextColor(Color.parseColor("#3f4032"));
                holder.progress.setText("当前");
            } else {
                holder.progress.setTextColor(Color.parseColor("#aeaca2"));
                float p = 0;
                if (AllCharNum > 0) {
                    p = (float) chapter.getStartIndex() / (float) AllCharNum;
                    if (p > 1) {
                        p = 1;
                    }
                }
                holder.progress.setText((int) (p * 100) + "%");
            }

            holder.index.setText(position + 1 + "");
            holder.title.setText((chapter.getTitle() + "").trim());
            holder.mGen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClick.onClick(v, chapter);
                }
            });
        }


        @Override
        public int getItemCount() {
            return mChapters == null ? 0 : mChapters.size();
        }

        private class Holder extends RecyclerView.ViewHolder {
            TextView       index;
            TextView       title;
            TextView       progress;
            RelativeLayout mGen;

            public Holder(View itemView) {
                super(itemView);
                mGen = itemView.findViewById(R.id.adapter_chatperlist_gen);
                index = itemView.findViewById(R.id.adapter_chatperlist_index);
                title = itemView.findViewById(R.id.adapter_chatperlist_title);
                progress = itemView.findViewById(R.id.adapter_chatperlist_progress);
            }
        }
    }


    public void onDestroy() {
        mContext = null;
        mRootView = null;
        mAdapter = null;
        if (mChapters != null) {
            mChapters.clear();
            mChapters = null;
        }
    }

    public interface OnItemClickListener {
        void onClick(View view, IChapter chapter);
    }

}
